package org.wmq.taskschedule.algorithm;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

/**
 * 图像锐化算法执行器
 * 实现AlgorithmExecutor接口，用于对图像进行锐化处理
 */
public class ImageSharpeningExecutor implements AlgorithmExecutor {

    private double alpha; // 锐化系数
    private double beta; // 原始图像权重
    private int kernelSize; // 核大小
    private String description; // 算法描述

    /**
     * 默认构造函数，使用默认参数
     */
    public ImageSharpeningExecutor() {
        this.alpha = 1.5;
        this.beta = -0.5;
        this.kernelSize = 3;
        this.description = "图像锐化算法 - 增强图像边缘和细节";
    }

    /**
     * 构造函数，允许自定义参数
     * 
     * @param alpha 锐化系数
     * @param beta 原始图像权重
     * @param kernelSize 核大小
     */
    public ImageSharpeningExecutor(double alpha, double beta, int kernelSize) {
        this.alpha = alpha;
        this.beta = beta;
        this.kernelSize = kernelSize;
        this.description = "图像锐化算法 - 增强图像边缘和细节";
    }

    /**
     * 构造函数，允许自定义所有参数
     * 
     * @param alpha 锐化系数
     * @param beta 原始图像权重
     * @param kernelSize 核大小
     * @param description 算法描述
     */
    public ImageSharpeningExecutor(double alpha, double beta, int kernelSize, String description) {
        this.alpha = alpha;
        this.beta = beta;
        this.kernelSize = kernelSize;
        this.description = description;
    }

    @Override
    public Mat execute(Mat input) {
        if (input == null || input.empty()) {
            throw new IllegalArgumentException("输入图像不能为空");
        }

        // 创建输出图像
        Mat output = new Mat();

        // 应用拉普拉斯锐化算法
        Mat blurred = new Mat();
        Imgproc.GaussianBlur(input, blurred, new org.opencv.core.Size(kernelSize, kernelSize), 0);

        Mat weightedInput = new Mat();
        Mat weightedBlurred = new Mat();

// input * alpha
        input.convertTo(weightedInput, input.type(), alpha, 0);
// blurred * beta
        blurred.convertTo(weightedBlurred, blurred.type(), beta, 0);

// 加权和
        Core.add(weightedInput, weightedBlurred, output);

// 释放资源
        weightedInput.release();
        weightedBlurred.release();

        // 释放中间资源
        blurred.release();

        return output;
    }

    // Getters and setters
    public double getAlpha() {
        return alpha;
    }

    public void setAlpha(double alpha) {
        this.alpha = alpha;
    }

    public double getBeta() {
        return beta;
    }

    public void setBeta(double beta) {
        this.beta = beta;
    }

    public int getKernelSize() {
        return kernelSize;
    }

    public void setKernelSize(int kernelSize) {
        this.kernelSize = kernelSize;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}